﻿using System;
using System.Text;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Data;
using FZKJ.DBUtility;
namespace FZKJ.DAL
{
    //sys_rolemenu
    public partial class sys_rolemenu
    {
        private readonly SQLHelper sqldal = null;
        public sys_rolemenu(string connStr = "")
        {
            sqldal = new SQLHelper(connStr);
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from sys_rolemenu");
            strSql.Append(" where ");
            strSql.Append(" id = @id  ");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
			};
            parameters[0].Value = id;

            return sqldal.Exists(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(FZKJ.Model.sys_rolemenu model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into sys_rolemenu(");
            strSql.Append("rolecode,menucode,btncode,createtime,createman");
            strSql.Append(") values (");
            strSql.Append("@rolecode,@menucode,@btncode,@createtime,@createman");
            strSql.Append(") ");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
			            new SqlParameter("@rolecode", model.rolecode) ,            
                        new SqlParameter("@menucode", model.menucode) ,            
                        new SqlParameter("@btncode", model.btncode) ,            
                        new SqlParameter("@createtime", model.createtime) ,            
                        new SqlParameter("@createman", model.createman)             
              
            };
            object obj = sqldal.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        /// <summary>
        /// 添加角色权限
        /// </summary>
        /// <param name="rolecode">角色编号</param>
        /// <param name="r_str">菜单按钮编号字符串</param>
        /// <returns></returns>
        public int SaveRoleMenu(string rolecode, string r_str)
        {
            List<CommandInfo> cmd_list = new List<CommandInfo>();
            string[] mb_list = r_str.Split('|');
            cmd_list.Add(new CommandInfo("delete from sys_rolemenu where rolecode='" + rolecode + "'", null));
            string t_sql = "insert into sys_rolemenu(rolecode,menucode,btncode) values(@rolecode,@menucode,@btncode)";
            foreach (string mb in mb_list)
            {
                string menucode = mb.Split('@')[0].ToString();
                string btncodelist = mb.Split('@')[1].ToString();
                SqlParameter[] p ={
                    new SqlParameter("@rolecode",rolecode),
                    new SqlParameter("@menucode",menucode),
                    new SqlParameter("@btncode",btncodelist)
                };
                cmd_list.Add(new CommandInfo(t_sql, p));
            }
            return new SQLHelper().ExecuteSqlTran(cmd_list);
        }

        /// <summary>
        /// 获取用户角色权限按钮
        /// </summary>
        /// <param name="rolecode"></param>
        /// <param name="menucode"></param>
        /// <returns></returns>
        public string GetRoleMenuButton(string rolecode, string menucode)
        {
            string t_sql = "select btncode from sys_rolemenu where rolecode='" + rolecode + "' and menucode='" + menucode + "'";
            DataTable dt = new SQLHelper().Query(t_sql).Tables[0];
            if (dt != null && dt.Rows.Count == 1)
            {
                string btn_code_list = string.Empty;
                foreach (string btncode in dt.Rows[0]["btncode"].ToString().Trim().Split(','))
                {
                    btn_code_list += "'" + btncode.Trim() + "',";
                }
                t_sql = "select btnmethod from sys_buttoninfo where isenable=1 and btncode in(" + btn_code_list.Substring(0, btn_code_list.Length - 1) + ") order by btnsort asc";
                DataTable b_dt = new SQLHelper().Query(t_sql).Tables[0];
                if (b_dt != null && b_dt.Rows.Count > 0)
                {
                    string btn_method = "";
                    for (int i = 0; i < b_dt.Rows.Count; i++)
                    {
                        if (i > 0)
                            btn_method += ",";
                        btn_method += b_dt.Rows[i]["btnmethod"].ToString();
                    }
                    return btn_method;
                }
                else
                    return string.Empty;
            }
            else
                return string.Empty;
        }

        /// <summary>
        /// 获取用户角色权限按钮
        /// </summary>
        /// <returns></returns>
        public DataTable GetRoleMenuButtonInfo(string rolecode, string menucode)
        {
            string t_sql = "select btncode from sys_rolemenu where rolecode='" + rolecode + "' and menucode='" + menucode + "'";
            DataTable dt = new SQLHelper().Query(t_sql).Tables[0];
            if (dt != null && dt.Rows.Count == 1)
            {
                string btn_code_list = string.Empty;
                foreach (string btncode in dt.Rows[0]["btncode"].ToString().Trim().Split(','))
                {
                    btn_code_list += "'" + btncode.Trim() + "',";
                }
                t_sql = "select btncode,btnname,btnclass,btnicon,btnmethod from sys_buttoninfo where isenable=1 and btncode in(" + btn_code_list.Substring(0, btn_code_list.Length - 1) + ") order by btnsort asc";
                return new SQLHelper().Query(t_sql).Tables[0];
            }
            else
                return null;
        }


        /// <summary>
        /// 更新一条数据
        /// </summary>
        public int Update(FZKJ.Model.sys_rolemenu model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update sys_rolemenu set ");

            strSql.Append(" rolecode = @rolecode , ");
            strSql.Append(" menucode = @menucode , ");
            strSql.Append(" btncode = @btncode , ");
            strSql.Append(" createtime = @createtime , ");
            strSql.Append(" createman = @createman  ");
            strSql.Append(" where id=@id ");

            SqlParameter[] parameters = {
			            new SqlParameter("@id", model.id ) ,            
                        new SqlParameter("@rolecode", model.rolecode ) ,            
                        new SqlParameter("@menucode", model.menucode ) ,            
                        new SqlParameter("@btncode", model.btncode ) ,            
                        new SqlParameter("@createtime", model.createtime ) ,            
                        new SqlParameter("@createman", model.createman )             
              
            };

            return sqldal.ExecuteSql(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from sys_rolemenu ");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
			};
            parameters[0].Value = id;


            int rows = sqldal.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 批量删除一批数据
        /// </summary>
        public bool DeleteList(string idlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from sys_rolemenu ");
            strSql.Append(" where ID in (" + idlist + ")  ");
            int rows = sqldal.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public FZKJ.Model.sys_rolemenu GetModel(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id, rolecode, menucode, btncode, createtime, createman  ");
            strSql.Append("  from sys_rolemenu ");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
			};
            parameters[0].Value = id;


            FZKJ.Model.sys_rolemenu model = new FZKJ.Model.sys_rolemenu();
            DataSet ds = sqldal.Query(strSql.ToString(), parameters);

            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["id"].ToString() != "")
                {
                    model.id = int.Parse(ds.Tables[0].Rows[0]["id"].ToString());
                }
                model.rolecode = ds.Tables[0].Rows[0]["rolecode"].ToString();
                model.menucode = ds.Tables[0].Rows[0]["menucode"].ToString();
                model.btncode = ds.Tables[0].Rows[0]["btncode"].ToString();
                if (ds.Tables[0].Rows[0]["createtime"].ToString() != "")
                {
                    model.createtime = DateTime.Parse(ds.Tables[0].Rows[0]["createtime"].ToString());
                }
                model.createman = ds.Tables[0].Rows[0]["createman"].ToString();

                return model;
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataTable GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM sys_rolemenu ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return sqldal.Query(strSql.ToString()).Tables[0];
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataTable GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" * ");
            strSql.Append(" FROM sys_rolemenu ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return sqldal.Query(strSql.ToString()).Tables[0];
        }


        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM sys_rolemenu ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = sqldal.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataTable GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.id desc");
            }
            strSql.Append(")AS Row, T.*  from sys_rolemenu T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return sqldal.Query(strSql.ToString()).Tables[0];
        }
    }
}